package com.hurui.xiaoming.adapter;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.bean.Li_MyList_Result;

import java.util.List;

/**
 * Created by hurui on 2017/7/16.
 */

public class DetailedListAdapter extends RecyclerView.Adapter<DetailedListAdapter.DetailedListHolder> {
    private Context mContext;
    private List<Li_MyList_Result.DataBean> lists;

    private DerailedListListener listener;
    String oistate = "";

    public void setListener(DerailedListListener listener) {
        this.listener = listener;
    }

    public DetailedListAdapter(Context mContext, List<Li_MyList_Result.DataBean> lists) {
        this.mContext = mContext;
        this.lists = lists;
    }


    public void refreshData(List<Li_MyList_Result.DataBean> data,String oi_state) {
        Log.i("状态值",oi_state + "------" + oi_state);
        if(oistate.equals(oi_state.trim())){
            lists.addAll(data);
        }else {
            lists = data;
        }
        this.notifyDataSetChanged();

    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(lists.get(position).getOi_id());
    }

    @Override
    public DetailedListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.fragment_detailed_list_item, parent, false);
        return new DetailedListHolder(itemView);
    }

    @Override
    public void onBindViewHolder(DetailedListHolder holder, final int position) {
        final Li_MyList_Result.DataBean dataBean = lists.get(position);
        switch (dataBean.getOi_state()) {
            case "1":
                //借款中
                holder.order_info_tv.setText("借款" + dataBean.getOi_jkprice() + "元，分" + dataBean.getOi_jkloans() + "期  " + dataBean.getNowloans()+"-" +dataBean.getOi_jkloans());
                holder.order_status_tv.setText("待还款");
                holder.order_money_tv.setText(dataBean.getHkzje());
                if (dataBean.getHktime()!=null)
                holder.order_time_tv.setText("还款日期: " + dataBean.getHktime());
                holder.order_btn.setText("立即还款");
                break;
            case "3":
                holder.order_info_tv.setText("借款" + dataBean.getOi_jkprice() + "元，分" + dataBean.getOi_jkloans() + "期  "+ dataBean.getNowloans()+"-" +dataBean.getOi_jkloans());
                holder.order_status_tv.setText("已还款");
                holder.order_money_tv.setText(dataBean.getHkzje());
                if (dataBean.getHktime()!=null)
                holder.order_time_tv.setText("还款日期: " + dataBean.getHktime());
                holder.order_btn.setVisibility(View.GONE);
                break;
            case "4":
                holder.order_info_tv.setText("借款" + dataBean.getOi_jkprice() + "元，分" + dataBean.getOi_jkloans() + "期  " + dataBean.getNowloans()+"-" +dataBean.getOi_jkloans());
                holder.order_status_tv.setText("已逾期");
                holder.order_money_tv.setText(dataBean.getHkzje());
                if (dataBean.getHktime()!=null)
                holder.order_time_tv.setText("还款日期: " + dataBean.getHktime());
                holder.order_btn.setText("立即还款");
                break;
            case "5":
                holder.order_info_tv.setText("借款" + dataBean.getOi_jkprice() + "元，分" + dataBean.getOi_jkloans() + "期"+ dataBean.getNowloans()+"-" +dataBean.getOi_jkloans());
                holder.order_status_tv.setText("已还款");
                holder.order_money_tv.setText(dataBean.getHkzje());
                if (dataBean.getHktime()!=null)
                holder.order_time_tv.setText("还款日期: " + dataBean.getHktime());
                holder.order_btn.setVisibility(View.GONE);
                break;
        }
        //立即还款
        holder.order_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                order_phone(dataBean);
            }
        });

        holder.order_info_open_rv.setTag(position);
        holder.order_info_open_rv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = (int) view.getTag();
                if (listener != null) {
                    listener.ClickInfoMore(pos);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class DetailedListHolder extends RecyclerView.ViewHolder {
        private LinearLayout order_info_open_rv;
        private TextView order_info_tv;//借款金额
        private TextView order_status_tv;
        private TextView order_money_tv;
        private TextView order_time_tv;
        private TextView order_btn;

        public DetailedListHolder(View itemView) {
            super(itemView);
            order_info_open_rv = (LinearLayout) itemView.findViewById(R.id.order_info_open_rv);
            order_info_tv = (TextView) itemView.findViewById(R.id.order_info_tv);
            order_status_tv = (TextView) itemView.findViewById(R.id.order_status_tv);
            order_money_tv = (TextView) itemView.findViewById(R.id.order_money_tv);
            order_time_tv = (TextView) itemView.findViewById(R.id.order_time_tv);
            order_btn = (TextView) itemView.findViewById(R.id.order_btn);
        }
    }

    public interface DerailedListListener {
        public void ClickInfoMore(int position);
    }

    AlertDialog alertDialog;
    public void setDialog() {
        View view = LayoutInflater.from(mContext.getApplicationContext()).inflate(R.layout.zhifubao, null);
        ImageView image = (ImageView) view.findViewById(R.id.zhifubaoImage);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });


        alertDialog = new AlertDialog.Builder(mContext)
                .setView(view)
                .create();
        alertDialog.show();
    }

    /**
     * 拨打电话
     * @param dataBean
     */
    private void order_phone(final Li_MyList_Result.DataBean dataBean) {

        AlertDialog.Builder localBuilder = new AlertDialog.Builder(mContext);
        localBuilder.setTitle("拨打电话");
        localBuilder.setMessage("+86" + dataBean.getUi_phone());
        /***
         * 设置点击返回键不会消失
         * */
        final AlertDialog dialog = localBuilder.setCancelable(false).create();

        localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + dataBean.getUi_phone()));
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                mContext.startActivity(intent);
            }
        });
        localBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                dialog.dismiss();
            }
        });

        localBuilder.show();
    }
}
